package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceCompanyDao;
import com.jiantong.work.model.yangzhibaoxian.InsuranceCheck;
import com.jiantong.work.model.yangzhibaoxian.InsuranceCompany;
import com.jiantong.work.service.yangzhibaoxian.InsuranceCompanyService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保险公司
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceCompanyServiceImpl implements InsuranceCompanyService {
    private InsuranceCompanyDao insuranceCompanyDao;

    @Autowired
    public InsuranceCompanyServiceImpl(InsuranceCompanyDao insuranceCompanyDao) {
        this.insuranceCompanyDao = insuranceCompanyDao;
    }
    @Override
    public Response<InsuranceCompany> findById(Long id) {
        try {
            InsuranceCompany insuranceCompany = (InsuranceCompany) insuranceCompanyDao.findById(id);
            if (insuranceCompany == null) {
                log.error("insuranceCompany(id={}) is not found", id);
                return Response.fail("insuranceCompany.not.found");
            }

            return Response.ok(insuranceCompany);
        } catch (Exception e) {
            log.error("failed to find insuranceCompany(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCompany.find.fail");
        }
    }

    @Override
    public Response<List<InsuranceCompany>> findByIds(List ids) {
        try {
            List<InsuranceCompany> insuranceCompanyList =  insuranceCompanyDao.findByIds(ids);
            if (insuranceCompanyList == null) {
                log.error("insuranceCompanyList(ids={}) is not found", ids);
                return Response.fail("insuranceCompanyList.not.found");
            }

            return Response.ok(insuranceCompanyList);
        } catch (Exception e) {
            log.error("failed to find insuranceCompanyList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCompanyList.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceCompany>> list(Map map) {
        try {
            List<InsuranceCompany> insuranceCompanyList = insuranceCompanyDao.list(map);
            if (insuranceCompanyList == null) {
                log.error("insuranceCompanyList(ids={}) is not found", map);
                return Response.fail("insuranceCompanyList.not.found");
            }

            return Response.ok(insuranceCompanyList);
        } catch (Exception e) {
            log.error("failed to find insuranceCompanyList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCompanyList.find.fail");
        }
    }

    @Override
    public Response<Paging<InsuranceCompany>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<InsuranceCompany> insuranceCompanyPaging =  insuranceCompanyDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), map);
            if (insuranceCompanyPaging == null) {
                log.error("insuranceCompanyPaging(ids={}) is not found", map);
                return Response.fail("insuranceCompanyPaging.not.found");
            }

            return Response.ok(insuranceCompanyPaging);
        } catch (Exception e) {
            log.error("failed to find insuranceCompanyPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceCompanyPaging.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceCompany insuranceCompany) {
        try {
            Boolean b =  insuranceCompanyDao.update(insuranceCompany);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(InsuranceCompany insuranceCompany) {
        try {
            insuranceCompanyDao.create( insuranceCompany);
            return  insuranceCompany.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceCompany> insuranceCompanyList) {
        try {

            return  insuranceCompanyDao.creates(insuranceCompanyList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

}
